package ibms_rcp.company.parts;

import ibms_rcp.dao.CompanyManager;
import ibms_rcp.dao.EstateManager;
import ibms_rcp.model.Building;
import ibms_rcp.model.Company;
import ibms_rcp.model.Floor;
import ibms_rcp.model.Lease;
import ibms_rcp.model.Room;
import ibms_rcp.verify.CompanyVerify;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class AddPart {
	// 公司名称
	private Text nameText;
	// 公司编号
	private Text codeText;
	// 联系人
	private Text perText;
	// 电话
	private Text telText;
	// 固定电话
	private Text ftelText;
	// 预存款
	private Text balanceText;
	// 提醒阈值
	private Spinner remindSpinner;
	//添加
	private Button addButton;
	//清空
	private Button clearButton;

	@Inject private MApplication application;
	@Inject private EPartService partService;
	@Inject private EModelService modelService;
	
	/**
	 * 构造函数 注入父组件
	 * 
	 * @param parent
	 */
	@Inject
	public AddPart(Composite parent) {
	}

	/**
	 * 添加控件
	 * 
	 * @param parent
	 */
	@PostConstruct
	public void createControls(final Composite parent) {
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		parent.setLayout(layout);
		
		Label title = new Label(parent, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setText("业主登记");
		
		Composite companyInfoComposite = new Composite(parent, SWT.NONE);
		companyInfoComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		companyInfoComposite.setLayout(new GridLayout(6, false));

		// 公司名称
		Label nameLabel1 = new Label(companyInfoComposite, SWT.RIGHT);
		nameLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		nameLabel1.setText("公司名称：");
		nameText = new Text(companyInfoComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));

		// 公司编号
		Label idLabel = new Label(companyInfoComposite, SWT.RIGHT);
		idLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		idLabel.setText("公司编号：");
		codeText = new Text(companyInfoComposite, SWT.BORDER);
		codeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		// 联系人
		Label perLabel = new Label(companyInfoComposite, SWT.RIGHT);
		perLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		perLabel.setText("联系人：");
		perText = new Text(companyInfoComposite, SWT.BORDER);
		perText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		// 电话
		Label telLabel = new Label(companyInfoComposite, SWT.RIGHT);
		telLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		telLabel.setText("电话：");
		telText = new Text(companyInfoComposite, SWT.BORDER);
		telText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		// 固定电话
		Label ftelLabel = new Label(companyInfoComposite, SWT.RIGHT);
		ftelLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		ftelLabel.setText("固定电话：");
		ftelText = new Text(companyInfoComposite, SWT.BORDER);
		ftelText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		// 预存款
		Label balanceLabel1 = new Label(companyInfoComposite, SWT.RIGHT);
		balanceLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		balanceLabel1.setText("预存款：");
		balanceText = new Text(companyInfoComposite, SWT.BORDER);
		balanceText.setText("0");
		balanceText.setEditable(false);
		balanceText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		Label balanceLabel2 = new Label(companyInfoComposite, SWT.LEFT);
		balanceLabel2.setText("元");

		// 提醒阈值
		Composite remindComposite = new Composite(companyInfoComposite, SWT.NONE);
		remindComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));
		remindComposite.setLayout(new GridLayout(3, false));
		Label remindLabel1 = new Label(remindComposite, SWT.RIGHT);
		remindLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		remindLabel1.setText("余额小于");
		remindSpinner = new Spinner(remindComposite, SWT.BORDER);
		Label remindLabel2 = new Label(remindComposite, SWT.LEFT);
		remindLabel2.setText("天开销时提醒充值");
		
		Composite buttonComposite = new Composite(parent, SWT.NONE);
		buttonComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		buttonComposite.setLayout(new GridLayout(2, false));
		
		addButton = new Button(buttonComposite, SWT.NONE);
		addButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击添加时
				System.out.println("addButton mouse up.");
				//添加公司信息
				Company company = new Company();
				addCompanyInfo(company);
				//company信息验证
				String message = CompanyVerify.addCompanyVerify(company);
				//如果是SUCCESS则添加，不是则显示警告信息
				if(message.equals("SUCCESS")){
					CompanyManager.save(company);//保存company
					application.getContext().set("company", company);//传递company参数
					swicthToInfoPerspective();//切换到company info perspective
				}
				else{
					MessageDialog.openWarning(parent.getShell(), "警告", message);
				}
			}
		});
		addButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		addButton.setText("添加");

		// 清空按钮， 一键清空表单项，将填写的信息完全从界面上清空，方便用户重新输入
		clearButton = new Button(buttonComposite, SWT.NONE);
		clearButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击清空时
				clearInput();
			}
		});
		clearButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		clearButton.setText("清空");

		System.out.println("AddPart createControls called.");
	}
	
	/**
	 * 清空输入
	 */
	@Focus
	public void clearInput(){
		nameText.setText("");
		codeText.setText("");
		perText.setText("");
		telText.setText("");
		ftelText.setText("");
		balanceText.setText("0");
		remindSpinner.setSelection(0);
	}
	
	@PreDestroy
	public void dispose() {
		System.out.println("AddPart dispose called.");
	}

	/**
	 * 添加公司信息
	 */
	public void addCompanyInfo(Company company){
		company.setName(nameText.getText().trim());
		company.setCode(codeText.getText().trim());
		company.setPer(perText.getText().trim());
		company.setTel(telText.getText().trim());
		company.setPhone(ftelText.getText().trim());
		company.setBalance(Double.parseDouble(balanceText.getText().trim()));
		company.setRemindDay(Integer.parseInt(remindSpinner.getText()));
	}
	
	/**
	 * 切换到company info perspective
	 */
	public void swicthToInfoPerspective(){
		//清空
//		clearInput();
		MPerspective element = (MPerspective) modelService.find("ibms_rcp.perspective.company.info", application);
		// Now switch perspective
		partService.switchPerspective(element);
	}
}
